package com.zx.BookSeaReader.ui.downloadbook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.base.BaseActivity;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookLabelBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.BookService;
import com.zx.BookSeaReader.ui.bookinfo.BookInfoActivity;
import com.zx.BookSeaReader.ui.read.ReadActivity;
import com.zx.BookSeaReader.widget.RefreshLayout;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;

import java.util.List;

public class DownLoadBooksActivity extends BaseActivity {

    @BindView(R.id.refresh_layout)
    RefreshLayout mRlRefresh;
    @BindView(R.id.refresh_rv_content)
    RecyclerView mRvBook;
    @BindView(R.id.tv_title_text)
    TextView mTvTitle;
    @BindView(R.id.ll_title_back)
    LinearLayout llTitleBack;

    private UserBean userBean;
    private Book book;
    private BookService mBookService;
    private List<Book> downLoadBooks;
    private DownLoadListBooksAdapter mDownLoadListBooksAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_down_load_books);
        ButterKnife.bind(this);
        mDownLoadListBooksAdapter = new DownLoadListBooksAdapter();
        mBookService=new BookService();
        mTvTitle.setText("下载列表");
        userBean = (UserBean)getIntent().getSerializableExtra(APPCONST.USER);
//        book=(Book)getIntent().getSerializableExtra(APPCONST.BOOK);
        downLoadBooks=mBookService.getDownloadBooks();

        mRvBook.setLayoutManager(new LinearLayoutManager(this));
        mRvBook.addItemDecoration(new DividerItemDecoration(this));
        mRvBook.setAdapter(mDownLoadListBooksAdapter);
        mRlRefresh.setBackground(ContextCompat.getDrawable(this,R.color.white));
        //mRlRefresh.setVisibility(View.GONE);
        mRlRefresh.setVisibility(View.VISIBLE);
        mRlRefresh.showFinish();
        mDownLoadListBooksAdapter.refreshItems(downLoadBooks);
        initClick();
    }

    protected void initClick() {

        llTitleBack.setOnClickListener(
                (v) ->{
                    finish();
                }
        );

        //书签的点击事件
        mDownLoadListBooksAdapter.setOnItemClickListener(
                (view, pos) -> {
                    Book book = mDownLoadListBooksAdapter.getItem(pos);
                    Intent intent = new Intent(this, ReadActivity.class);
                    intent.putExtra(APPCONST.BOOK, book);
                    intent.putExtra(APPCONST.USER, userBean);
                    startActivity(intent);

                }
        );

    }
}
